home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH4 / SRC / BRIGHT.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-01-03  |  18.9 KB  |  600 lines

  1. VERSION 4.00
  2. Begin VB.Form BrightForm 
  3.    Caption         =   "Brightness"
  4.    ClientHeight    =   4470
  5.    ClientLeft      =   1380
  6.    ClientTop       =   915
  7.    ClientWidth     =   4695
  8.    Height          =   5160
  9.    Left            =   1320
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   298
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   313
  14.    Top             =   285
  15.    Width           =   4815
  16.    Begin VB.PictureBox BalPict 
  17.       BorderStyle     =   0  'None
  18.       Height          =   255
  19.       Left            =   0
  20.       ScaleHeight     =   255
  21.       ScaleWidth      =   4695
  22.       TabIndex        =   4
  23.       Top             =   4200
  24.       Width           =   4695
  25.       Begin VB.HScrollBar ScrollBright 
  26.          Enabled         =   0   'False
  27.          Height          =   255
  28.          LargeChange     =   10
  29.          Left            =   380
  30.          Max             =   100
  31.          Min             =   -100
  32.          TabIndex        =   5
  33.          Top             =   0
  34.          Width           =   4350
  35.       End
  36.       Begin VB.Label ValueBright 
  37.          BorderStyle     =   1  'Fixed Single
  38.          Caption         =   "0"
  39.          Height          =   255
  40.          Left            =   0
  41.          TabIndex        =   6
  42.          Top             =   0
  43.          Width           =   375
  44.       End
  45.    End
  46.    Begin VB.PictureBox FromSwin 
  47.       Height          =   3855
  48.       Left            =   0
  49.       ScaleHeight     =   253
  50.       ScaleMode       =   3  'Pixel
  51.       ScaleWidth      =   293
  52.       TabIndex        =   2
  53.       Top             =   0
  54.       Width           =   4455
  55.       Begin VB.PictureBox FromPict 
  56.          AutoRedraw      =   -1  'True
  57.          AutoSize        =   -1  'True
  58.          Height          =   1905
  59.          Left            =   0
  60.          ScaleHeight     =   123
  61.          ScaleMode       =   3  'Pixel
  62.          ScaleWidth      =   88
  63.          TabIndex        =   3
  64.          Top             =   0
  65.          Width           =   1380
  66.       End
  67.    End
  68.    Begin VB.HScrollBar FromHBar 
  69.       Enabled         =   0   'False
  70.       Height          =   255
  71.       Left            =   0
  72.       TabIndex        =   1
  73.       Top             =   3840
  74.       Width           =   4485
  75.    End
  76.    Begin VB.VScrollBar FromVBar 
  77.       Enabled         =   0   'False
  78.       Height          =   3855
  79.       Left            =   4440
  80.       TabIndex        =   0
  81.       Top             =   0
  82.       Width           =   255
  83.    End
  84.    Begin MSComDlg.CommonDialog FileDialog 
  85.       Left            =   4200
  86.       Top             =   3600
  87.       _Version        =   65536
  88.       _ExtentX        =   847
  89.       _ExtentY        =   847
  90.       _StockProps     =   0
  91.       CancelError     =   -1  'True
  92.    End
  93.    Begin VB.Menu mnuFile 
  94.       Caption         =   "&File"
  95.       Begin VB.Menu mnuFileLoad 
  96.          Caption         =   "&Load..."
  97.          Shortcut        =   ^L
  98.       End
  99.       Begin VB.Menu mnuFileSep2 
  100.          Caption         =   "-"
  101.       End
  102.       Begin VB.Menu mnuFileExit 
  103.          Caption         =   "E&xit"
  104.       End
  105.    End
  106. Attribute VB_Name = "BrightForm"
  107. Attribute VB_Creatable = False
  108. Attribute VB_Exposed = False
  109. Option Explicit
  110. Dim SysPalSize As Integer
  111. Dim NumStaticColors As Integer
  112. Dim StaticColor1 As Integer
  113. Dim StaticColor2 As Integer
  114. Dim bytes() As Byte
  115. Dim wid As Long
  116. Dim hgt As Long
  117. Dim LogPal As Integer
  118. Dim origpal(0 To 255) As PALETTEENTRY
  119. Dim newpal(0 To 255) As PALETTEENTRY
  120. Dim SettingValues As Boolean
  121. ' ***********************************************
  122. ' Load the control's palette so the non-static
  123. ' colors are grays. Map the logical palette to
  124. ' match the system palette. Convert the image to
  125. ' use the non-static grays.
  126. ' Set the following module global variables.
  127. '   LogPal      Image logical palette handle.
  128. '   origpal()  Image logical palette entries.
  129. '   wid         Width of image.
  130. '   hgt         Height of image.
  131. '   bytes(1 To wid, 1 To hgt)
  132. '               Image pixel values.
  133. ' ***********************************************
  134. Sub MatchGrayPalette(pic As Control)
  135. Dim sys(0 To 255) As PALETTEENTRY
  136. Dim i As Integer
  137. Dim bm As BITMAP
  138. Dim hbm As Integer
  139. Dim status As Long
  140. Dim X As Integer
  141. Dim Y As Integer
  142. Dim gray As Single
  143. Dim dgray As Single
  144. Dim c As Integer
  145. Dim clr As Integer
  146.     ' Make sure pic has the foreground palette.
  147.     pic.ZOrder
  148.     i = RealizePalette(pic.hdc)
  149.     DoEvents
  150.     ' Get the system palette entries.
  151.     i = GetSystemPaletteEntries(pic.hdc, 0, SysPalSize, sys(0))
  152.         
  153.     ' Get the image pixels.
  154.     hbm = pic.Image
  155.     status = GetObject(hbm, BITMAP_SIZE, bm)
  156.     wid = bm.bmWidthBytes
  157.     hgt = bm.bmHeight
  158.     ReDim bytes(1 To wid, 1 To hgt)
  159.     status = GetBitmapBits(hbm, wid * hgt, bytes(1, 1))
  160.     ' Make the logical palette as big as possible.
  161.     LogPal = pic.Picture.hPal
  162.     If ResizePalette(LogPal, SysPalSize) = 0 Then
  163.         Beep
  164.         MsgBox "Error resizing logical palette.", _
  165.             vbExclamation
  166.         Exit Sub
  167.     End If
  168.     ' Blank the non-static colors.
  169.     For i = 0 To StaticColor1
  170.         origpal(i) = sys(i)
  171.     Next i
  172.     For i = StaticColor1 + 1 To StaticColor2 - 1
  173.         With origpal(i)
  174.             .peRed = 0
  175.             .peGreen = 0
  176.             .peBlue = 0
  177.             .peFlags = PC_NOCOLLAPSE
  178.         End With
  179.     Next i
  180.     For i = StaticColor2 To 255
  181.         origpal(i) = sys(i)
  182.     Next i
  183.     i = SetPaletteEntries(LogPal, 0, SysPalSize, origpal(0))
  184.     ' Insert the non-static grays.
  185.     gray = 0
  186.     dgray = 255 / (StaticColor2 - StaticColor1 - 2)
  187.     For i = StaticColor1 + 1 To StaticColor2 - 1
  188.         c = gray
  189.         gray = gray + dgray
  190.         With origpal(i)
  191.             .peRed = c
  192.             .peGreen = c
  193.             .peBlue = c
  194.         End With
  195.     Next i
  196.     i = SetPaletteEntries(LogPal, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, origpal(StaticColor1 + 1))
  197.     ' Recreate the image using the new colors.
  198.     For Y = 1 To hgt
  199.         For X = 1 To wid
  200.             clr = bytes(X, Y)
  201.             With sys(clr)
  202.                 c = (CInt(.peRed) + .peGreen + .peBlue) / 3
  203.             End With
  204.             bytes(X, Y) = NearestNonstaticGray(c)
  205.         Next X
  206.     Next Y
  207.     status = SetBitmapBits(hbm, wid * hgt, bytes(1, 1))
  208.     ' Realize the gray palette.
  209.     i = RealizePalette(pic.hdc)
  210.     pic.Refresh
  211. End Sub
  212. ' ************************************************
  213. ' Return the index of the nonstatic color closest
  214. ' to the given color value.
  215. ' ************************************************
  216. Function NearestNonstaticColor(ByVal r As Integer, ByVal g As Integer, ByVal b As Integer) As Integer
  217. Dim best_i As Integer
  218. Dim best_dist As Long
  219. Dim dist As Long
  220. Dim dr As Long
  221. Dim dg As Long
  222. Dim db As Long
  223. Dim i As Integer
  224.     best_dist = 1000000
  225.     For i = StaticColor1 + 1 To StaticColor2 - 1
  226.         With origpal(i)
  227.             dr = r - .peRed
  228.             dg = g - .peGreen
  229.             db = b - .peBlue
  230.             dist = dr * dr + dg * dg + db * db
  231.         End With
  232.         If best_dist > dist Then
  233.             best_i = i
  234.             best_dist = dist
  235.         End If
  236.     Next i
  237.     NearestNonstaticColor = best_i
  238. End Function
  239. ' ************************************************
  240. ' Return the index of the nonstatic gray closest
  241. ' to the given value (assuming the non-static
  242. ' colors are a gray scale created by
  243. ' MatchGrayPalette).
  244. ' ************************************************
  245. Function NearestNonstaticGray(c As Integer) As Integer
  246. Dim dgray As Single
  247.     If c < 0 Then
  248.         c = 0
  249.     ElseIf c > 255 Then
  250.         c = 255
  251.     End If
  252.     dgray = 255 / (StaticColor2 - StaticColor1 - 2)
  253.     NearestNonstaticGray = c / dgray + StaticColor1 + 1
  254. End Function
  255. ' ***********************************************
  256. ' Load the control's palette so it matches the
  257. ' the system palette. Remap any of the image's
  258. ' pixels that use static colors to non-static
  259. ' colors.
  260. ' Set the following module global variables.
  261. '   LogPal      Image logical palette handle.
  262. '   origpal()  Image logical palette entries.
  263. '   wid         Width of image.
  264. '   hgt         Height of image.
  265. '   bytes(1 To wid, 1 To hgt)
  266. '               Image pixel values.
  267. ' ***********************************************
  268. Sub MatchColorPalette(pic As Control)
  269. Dim sys(0 To 255) As PALETTEENTRY
  270. Dim i As Integer
  271. Dim bm As BITMAP
  272. Dim hbm As Integer
  273. Dim status As Long
  274. Dim X As Integer
  275. Dim Y As Integer
  276. Dim clr As Integer
  277.     ' Make sure pic has the foreground palette.
  278.     pic.ZOrder
  279.     i = RealizePalette(pic.hdc)
  280.     DoEvents
  281.     ' Get the system palette entries.
  282.     i = GetSystemPaletteEntries(pic.hdc, 0, SysPalSize, sys(0))
  283.             
  284.     ' Make the logical palette as big as possible.
  285.     LogPal = pic.Picture.hPal
  286.     If ResizePalette(LogPal, SysPalSize) = 0 Then
  287.         Beep
  288.         MsgBox "Error resizing logical palette.", _
  289.             vbExclamation
  290.         Exit Sub
  291.     End If
  292.     ' Blank the non-static colors.
  293.     For i = 0 To StaticColor1
  294.         origpal(i) = sys(i)
  295.     Next i
  296.     For i = StaticColor1 + 1 To StaticColor2 - 1
  297.         With origpal(i)
  298.             .peRed = 0
  299.             .peGreen = 0
  300.             .peBlue = 0
  301.             .peFlags = PC_NOCOLLAPSE
  302.         End With
  303.     Next i
  304.     For i = StaticColor2 To 255
  305.         origpal(i) = sys(i)
  306.     Next i
  307.     i = SetPaletteEntries(LogPal, 0, SysPalSize, origpal(0))
  308.     ' Insert the non-static colors.
  309.     For i = StaticColor1 + 1 To StaticColor2 - 1
  310.         origpal(i) = sys(i)
  311.         origpal(i).peFlags = PC_NOCOLLAPSE
  312.     Next i
  313.     i = SetPaletteEntries(LogPal, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, origpal(StaticColor1 + 1))
  314.     ' Realize the new palette.
  315.     i = RealizePalette(pic.hdc)
  316.     ' Get the image pixels.
  317.     hbm = pic.Image
  318.     status = GetObject(hbm, BITMAP_SIZE, bm)
  319.     wid = bm.bmWidthBytes
  320.     hgt = bm.bmHeight
  321.     ReDim bytes(1 To wid, 1 To hgt)
  322.     status = GetBitmapBits(hbm, wid * hgt, bytes(1, 1))
  323.     ' Remap any pixels using static colors.
  324.     For Y = 1 To hgt
  325.         For X = 1 To wid
  326.             clr = bytes(X, Y)
  327.             If clr <= StaticColor1 Or clr >= StaticColor2 Then
  328.                 With sys(clr)
  329.                     bytes(X, Y) = _
  330.                         NearestNonstaticColor( _
  331.                         .peRed, .peGreen, .peBlue)
  332.                 End With
  333.             End If
  334.         Next X
  335.     Next Y
  336.     ' Update the image's pixel values.
  337.     status = SetBitmapBits(hbm, wid * hgt, bytes(1, 1))
  338.     pic.Refresh
  339. End Sub
  340. ' ***********************************************
  341. ' Load the indicated file and prepare to work
  342. ' with its palette.
  343. ' ***********************************************
  344. Sub LoadFromPict(fname As String)
  345. Dim status As Long
  346. Dim i As Integer
  347.     On Error GoTo LoadFileError
  348.     FromPict.Picture = LoadPicture(fname)
  349.     On Error GoTo 0
  350.         
  351.     FromHBar.Enabled = False
  352.     FromVBar.Enabled = False
  353.     ScrollBright.Enabled = False
  354.     DoEvents
  355.     MatchColorPalette FromPict
  356.     For i = 0 To SysPalSize - 1
  357.         newpal(i) = origpal(i)
  358.     Next i
  359.     FromPict.Move 0, 0
  360.     ResetScrollBars
  361.     ScrollBright.Enabled = True
  362.     SettingValues = True
  363.     ScrollBright.Value = 0
  364.     SettingValues = False
  365.         
  366.     Caption = "Brightness [" & fname & "]"
  367.     Exit Sub
  368. LoadFileError:
  369.     Beep
  370.     MsgBox "Error loading file " & fname & "." & _
  371.         vbCrLf & Error$
  372.     Exit Sub
  373. End Sub
  374. ' ***********************************************
  375. ' Set the Max and LargeChange properties for the
  376. ' image scroll bars.
  377. ' ***********************************************
  378. Sub ResetScrollBars()
  379.     ' FromHBar.
  380.     FromHBar.Value = 0
  381.     If FromSwin.ScaleWidth >= FromPict.Width Then
  382.         FromHBar.Enabled = False
  383.     Else
  384.         FromHBar.Max = FromPict.Width - FromSwin.ScaleWidth
  385.         FromHBar.LargeChange = FromSwin.ScaleWidth
  386.         FromHBar.Enabled = True
  387.     End If
  388.     ' FromVBar.
  389.     FromVBar.Value = 0
  390.     If FromSwin.ScaleHeight >= FromPict.Height Then
  391.         FromVBar.Enabled = False
  392.     Else
  393.         FromVBar.Max = FromPict.Height - FromSwin.ScaleHeight
  394.         FromVBar.LargeChange = FromSwin.ScaleHeight
  395.         FromVBar.Enabled = True
  396.     End If
  397. End Sub
  398. ' ************************************************
  399. ' Adjust the brightness of the colors.
  400. ' If -100 <= v <= 0, then subtract v percent of
  401. ' the color components.
  402. ' If 0 <= v <= 100, then add v percent of the
  403. ' difference between the color components and 255.
  404. ' Thus:
  405. '   When v = -100, the color goes to 0.
  406. '   When v =    0, the color is unchanged.
  407. '   When v =  100, the color goes to 255.
  408. ' ************************************************
  409. Sub UpdateColors()
  410. Dim i As Integer
  411. Dim v As Single
  412. Dim f As Single
  413. Dim r As Integer
  414. Dim g As Integer
  415. Dim b As Integer
  416.     ' Get the scroll bar value.
  417.     v = ScrollBright.Value / 100#
  418.     If v < 0 Then
  419.         f = 1 + v
  420.     Else
  421.         f = v
  422.     End If
  423.     ' Set the new palette entries.
  424.     For i = StaticColor1 + 1 To StaticColor2 - 1
  425.         With origpal(i)
  426.             r = .peRed
  427.             g = .peGreen
  428.             b = .peBlue
  429.             If v < 0 Then
  430.                 r = r * f
  431.                 g = g * f
  432.                 b = b * f
  433.             Else
  434.                 r = r + (255 - r) * f
  435.                 g = g + (255 - g) * f
  436.                 b = b + (255 - b) * f
  437.             End If
  438.         End With
  439.         With newpal(i)
  440.             .peRed = r
  441.             .peGreen = g
  442.             .peBlue = b
  443.             .peFlags = PC_NOCOLLAPSE
  444.         End With
  445.     Next i
  446.     i = SetPaletteEntries(LogPal, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, newpal(StaticColor1 + 1))
  447.     i = RealizePalette(FromPict.hdc)
  448. End Sub
  449. ' ***********************************************
  450. ' Give the form and all the picture boxes an
  451. ' hourglass cursor.
  452. ' ***********************************************
  453. Sub WaitStart()
  454.     MousePointer = vbHourglass
  455.     FromPict.MousePointer = vbHourglass
  456.     DoEvents
  457. End Sub
  458. ' ***********************************************
  459. ' Restore the mouse pointers for the form and all
  460. ' the picture boxes.
  461. ' ***********************************************
  462. Sub WaitEnd()
  463.     MousePointer = vbDefault
  464.     FromPict.MousePointer = vbDefault
  465. End Sub
  466. Private Sub Form_Load()
  467.     ' Make sure the screen supports palettes.
  468.     If Not GetDeviceCaps(hdc, RASTERCAPS) And RC_PALETTE Then
  469.         Beep
  470.         MsgBox "This monitor does not support palettes.", _
  471.             vbCritical
  472.         End
  473.     End If
  474.     ' Get system palette size and # static colors.
  475.     SysPalSize = GetDeviceCaps(hdc, SIZEPALETTE)
  476.     NumStaticColors = GetDeviceCaps(hdc, NUMRESERVED)
  477.     StaticColor1 = NumStaticColors \ 2 - 1
  478.     StaticColor2 = SysPalSize - NumStaticColors \ 2
  479.     ' Remove the borders from FromPict.
  480.     FromPict.BorderStyle = vbTransparent
  481.     ' Make sure FromPict has control.
  482.     FromPict.ZOrder
  483. End Sub
  484. ' ***********************************************
  485. ' Make the picture as large as possible.
  486. ' ***********************************************
  487. Private Sub Form_Resize()
  488. Const GAP = 6
  489. Dim hgt As Single
  490. Dim wid As Single
  491. Dim lft As Single
  492.     If WindowState = vbMinimized Then Exit Sub
  493.         
  494.     hgt = ScaleHeight - FromHBar.Height - 1 - _
  495.         BalPict.Height - 1.5 * GAP
  496.     wid = ScaleWidth - FromVBar.Width - 1
  497.     ' Place FromSwin and its scroll bars.
  498.     FromSwin.Move 0, 0, wid, hgt
  499.     FromVBar.Move _
  500.         FromSwin.Left + FromSwin.Width + 1, _
  501.         0, FromVBar.Width, hgt
  502.     FromHBar.Move _
  503.         FromSwin.Left, FromSwin.Height + 1, _
  504.         wid
  505.         
  506.     BalPict.Move 0, _
  507.         FromHBar.Top + FromHBar.Height + GAP, _
  508.         ScaleWidth
  509.     ScrollBright.Width = BalPict.ScaleWidth - ScrollBright.Left
  510.     ResetScrollBars
  511. End Sub
  512. Private Sub Form_Unload(Cancel As Integer)
  513.     End
  514. End Sub
  515. ' ***********************************************
  516. ' Move FromPict within FromSwin.
  517. ' ***********************************************
  518. Private Sub FromHBar_Change()
  519.     FromPict.Left = -FromHBar.Value
  520. End Sub
  521. ' ***********************************************
  522. ' Move FromPict within FromSwin.
  523. ' ***********************************************
  524. Private Sub FromHBar_Scroll()
  525.     FromPict.Left = -FromHBar.Value
  526. End Sub
  527. ' ************************************************
  528. ' Present a message indicating the pixel's palette
  529. ' index and color value.
  530. ' ************************************************
  531. Private Sub FromPict_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  532.     If X > wid Or Y > hgt Then Exit Sub
  533.     With newpal(bytes(X, Y))
  534.         MsgBox "Palette index:" & Str$(bytes(X, Y)) & _
  535.             vbCrLf & "Red:  " & Str$(.peRed) & _
  536.             vbCrLf & "Green:" & Str$(.peGreen) & _
  537.             vbCrLf & "Blue: " & Str$(.peBlue)
  538.     End With
  539. End Sub
  540. ' ***********************************************
  541. ' Load a new image file.
  542. ' ***********************************************
  543. Private Sub mnuFileLoad_Click()
  544. Dim fname As String
  545.     ' Allow the user to pick a file.
  546.     On Error Resume Next
  547.     FileDialog.filename = "*.BMP;*.ICO;*.RLE;*.WMF;*.DIB"
  548.     FileDialog.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  549.     FileDialog.ShowOpen
  550.     If Err.Number = cdlCancel Then
  551.         Exit Sub
  552.     ElseIf Err.Number <> 0 Then
  553.         Beep
  554.         MsgBox "Error selecting file.", , vbExclamation
  555.         Exit Sub
  556.     End If
  557.     On Error GoTo 0
  558.     fname = Trim$(FileDialog.filename)
  559.     FileDialog.InitDir = Left$(fname, Len(fname) _
  560.         - Len(FileDialog.FileTitle) - 1)
  561.     ' Load the picture.
  562.     WaitStart
  563.     DoEvents
  564.     LoadFromPict fname
  565.     WaitEnd
  566. End Sub
  567. ' ***********************************************
  568. ' End the application. (See also the QueryUnload
  569. ' event.)
  570. ' ***********************************************
  571. Private Sub mnuFileExit_Click()
  572.     Unload Me
  573. End Sub
  574. ' ***********************************************
  575. ' Move FromPict within FromSwin.
  576. ' ***********************************************
  577. Private Sub FromVBar_Change()
  578.     FromPict.Top = -FromVBar.Value
  579. End Sub
  580. ' ***********************************************
  581. ' Move FromPict within FromSwin.
  582. ' ***********************************************
  583. Private Sub FromVBar_Scroll()
  584.     FromPict.Top = -FromVBar.Value
  585. End Sub
  586. ' ************************************************
  587. ' Update the image brightness.
  588. ' ************************************************
  589. Private Sub ScrollBright_Change()
  590.     ValueBright.Caption = Format$(ScrollBright.Value)
  591.     If SettingValues Then Exit Sub
  592.     UpdateColors
  593. End Sub
  594. ' ************************************************
  595. ' Update the image brightness.
  596. ' ************************************************
  597. Private Sub ScrollBright_Scroll()
  598.     ScrollBright_Change
  599. End Sub
  600.